<?php


namespace app\admin\controller\business;


use app\common\controller\Adminbase;
use think\Db;

class Ebusiness extends Adminbase
{

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $_list = Db::name('business')
                ->where('status', 'in', [0, 3])    //0待审核 3审核失败
                ->where('is_del', '=', 0)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //获取数量
            $total = Db::name('business')
                ->where('status', 'in', [0, 3])    //0待审核 3审核失败
                ->where('is_del', '=', 0)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    //审核
    public function verify()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'status' => $data['row']['status'],
                'reason_refusal' => $data['row']['reason_refusal'],
                'commission' => $data['row']['commission'],
                'logo_url' => $data['row']['logo_url'],
                'business_img' => $data['row']['business_img'],
                'operate_type' => $data['row']['operate_type'],
                'brand' => $data['row']['brand'],
                'to_day' => $data['row']['to_day'],
                'min_tixian' => $data['row']['min_tixian'],
                'bank_name' => $data['row']['bank_name'],
                'bank_account' => $data['row']['bank_account'],
                'bank_account_name' => $data['row']['bank_account_name'],
                'finance_people' => $data['row']['finance_people'],
                'finance_phone' => $data['row']['finance_phone'],
            );

            //审核完成后生成商家二维码  //pages1/staffMessage/staffMessage
            if ($data['row']['status'] == 1) {
                //todo 小程序二维码路径不对
                $code_url=qrcode('business_id',$data['id'],'pages1/staffMessage/staffMessage');
//                $code_url = qrcode('business_id', $data['id'], '');

                $insert_data['code_url'] = $code_url;

                $shui_text = config('site.web_sy');
                $shui_color = config('site.web_sy_color');
                $fblUrl = replaceImgDomain($data['food_business_license']);
                $blUrl = replaceImgDomain($data['business_license']);

                //生成水印  business_license food_business_license
                $url = config('wechat.url');
                $food_business_license = $url.'/'.setSYText($fblUrl, $shui_text, $shui_color);
                $business_license = $url.'/'.setSYText($blUrl, $shui_text, $shui_color);
                $insert_data['food_business_license']=$food_business_license;
                $insert_data['business_license']=$business_license;
            }
            if ($data['row']['logo_url']) {
                $c1 = edit_img($data['row']['logo_url']);
                $c2 = c2($c1);
                $insert_data['logo_map'] = $c2;
            }
            $business_info = Db::name('business')
                ->where('id', '=', $data['id'])
                ->where('is_del', '=', 0)
                ->update($insert_data);
            $this->success('操作成功');
        } else {
            $business_id = $this->request->param('id/d', 0);
            $data = Db::name('business')->where('is_del', '=', 0)->where('id', $business_id)->find();
            //获取经营方式
            $operate_type_list = Db::name('operate_type')
                ->where('is_del', 0)
                ->order('sort desc')
                ->select();
            $brand_list = Db::name('brand')
                ->where('is_del', 0)
                ->order('sort desc')
                ->select();

            $this->assign([
                'data' => $data,
                'operate_type_list' => $operate_type_list,
                'brand_list' => $brand_list,
            ]);
            //operate_type   经营形式（ 单一）1独立经营    2连锁品牌    3加盟品牌
            //operate_cate varchar(20) DEFAULT NULL COMMENT '多分类 1,2,3 形式',
            //申请用户
            return $this->fetch();
        }
    }
}